/**
 *  PCFValidator - Portable Content Format validator
 *  Copyright (C) 2009 CEPHIS <http://cephis.uab.cat>
 *
 *  Developed by:
 *    Aitor Rodriguez Alsina <aitor.rodriguez@uab.cat>, CEPHIS
 *
 *  This file is part of PCFmodel, a free software library.
 *
 *  If you have no applicable agreement with CEPHIS, this software
 *  is licensed to you, and you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 3 of the License.
 *
 *  If you have a applicable agreement with CEPHIS, the terms of that
 *  agreement prevail.
 *
 *  PCFModel is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
package com.cephis.pcfjtools.validator;

import java.io.File;
import java.net.URL;
import javax.xml.validation.SchemaFactory;
import javax.xml.validation.Schema;
import javax.xml.XMLConstants;
import javax.xml.transform.sax.SAXSource;
import org.xml.sax.InputSource;
import javax.xml.validation.Validator;

/**
 *
 * @author Tots
 */
public class PCFValidator
{
    private URL schemaFile;

    public PCFValidator()
    {
        schemaFile = getClass().getResource("/pcfschema/pcf.xsd");
    }

    /**
     * XSD schema SAX validation
     * @param xmlFile The XML document file to be validated
     * @return
     */
    public boolean validates(String xmlFilePath)
    {
        return validates(new File(xmlFilePath));
    }

    /**
     * XSD schema SAX validation
     * @param xmlFile The XML document file to be validated
     * @return
     */
    public boolean validates(File xmlFile)
    {
        try
        {
            // loading the XML schema
            String language = XMLConstants.W3C_XML_SCHEMA_NS_URI;
            SchemaFactory factory = SchemaFactory.newInstance(language);
            Schema schema = factory.newSchema(schemaFile);

            // validating the XML file through a SAX source
            SAXSource source = new SAXSource(
                new InputSource(new java.io.FileInputStream(xmlFile)));
            Validator validator = schema.newValidator();
            validator.validate(source);
            return true;
        }
        catch (Exception e)
        {
            System.out.println(e.toString());
        }
        return false;
    }
}
